package artid.com.arttogoa.util;

import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

/*import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;*/
/**
 * 
 *    
 * 项目名称：Framework   
 * 类名称：GsonUtils   
 * 类描述：Gson解析框架   
 * 创建人：耿卫斌  
 * 创建时间：2015-2-3 下午1:53:44   
 * 修改备注：    HttpUtils http=new HttpUtils();
		RequestParams params = new RequestParams();
		params.addBodyParameter("address_id",address_id);
		http.send(HttpRequest.HttpMethod.POST, URL_Utils.COMMONADDRESS_DELETE, params ,new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				// TODO Auto-generated method stub
				ToastUtil.toast(mContext, "网络塞车，请稍重试!!");
				dissMissDialog();
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				// TODO Auto-generated method stub
				String json=arg0.result;
				String code = GsonUtils.code(json, "code");
				BaseBean mBaseBean = GsonUtils.json2bean(json, BaseBean.class);
				if (code.equalsIgnoreCase("1")) {
					page=1;
					isRefreshed=true;
					getData(user_id, page);
					ToastUtil.toast(mContext, mBaseBean.getMessage().toString());
				}else{
					ToastUtil.toast(mContext, mBaseBean.getMessage().toString());
				}
				dissMissDialog();
			}
		});

		//外层为数组类型
		 	jsonArray = new JSONArray(json);
					List<实体类> mBean=new ArrayList<实体类>();
					for(int i=0;i<jsonArray.length();i++){
						TestBean json2bean = GsonUtils.json2bean(jsonArray.opt(i).toString(), 实体类.class);
						mBean.add(json2bean);
						}
 *
 */
public class GsonUtils {
	public static <T> T json2bean(String result, Class<T> clazz) {

		Gson gson = new Gson();

		T t = gson.fromJson(result, clazz);

		return t;
	}
	
	//JsonArray转成集合
	public static List<?> json2List(String jsonStr) {
		Gson gson = new Gson();
        List<?> objList = null;
        if (gson != null) {
            Type type = new com.google.gson.reflect.TypeToken<List<?>>() {
            }.getType();
            objList = gson.fromJson(jsonStr, type);
        }
        return objList;
    }
	
	
	public static Map<?, ?> json2Map(String jsonStr) {
		Gson gson = new Gson();
        Map<?, ?> objMap = null;
        if (gson != null) {
            Type type = new com.google.gson.reflect.TypeToken<Map<?, ?>>() {
            }.getType();
            objMap = gson.fromJson(jsonStr, type);
        }
        return objMap;
    }

	public static JsonObject str2JosnObj(String result) {

		return new JsonParser().parse(result).getAsJsonObject();
	}

	public static int code(String result, String key) {

		return str2JosnObj(result).get(key).getAsInt();
	}
	
	public static String message(String result, String key) {
		
		return str2JosnObj(result).get(key).getAsString();
	}
	
	public static Boolean success(String result,String key){
		return str2JosnObj(result).get(key).getAsBoolean();
	}
	
	public static Long getLong(String result,String key){
		return str2JosnObj(result).get(key).getAsLong();
	}
	
}
